<?php
namespace Model;
require_once __DIR__."/../Lib/mydb.php";
require_once __DIR__."/../Utils/Contains.php";
require_once __DIR__."/../Utils/RongUtils.php";
use Lib\mydb;
use Utils\RongUtils;
use Model;
use Utils\Contains;
class User{
    private static $db=null;
    public $student_id;
    public $pwd;
    public $token;
    public $name;
    public $academy;
    public $professional;
    public $classroom;
    public function create($array) {
        $this->student_id=$array['id'];
        $this->pwd=$array['pwd'];
        $this->name=$array["name"];
        $this->academy=$array["academy"];
        $this->professional=$array["professional"];
        $this->classroom=$array["classroom"];;
    }
    

    public function getkey($array){
        if(self::$db==null)
            self::$db =new mydb();
        $sql="select * from user where student_id='".$array['id']."'and password = '".$array['pwd']."'and ischeckd='已审核';";
        $res=self::$db->qurey($sql);
      if($res){
        $dd=$res->fetch_assoc();
          $this->token=RongUtils::get($dd);
          $this->name=$dd["username"];
          $this->student_id=$dd["student_id"];
          $this->professional=$dd["professional"];
          $this->academy=$dd["academy"];
          $this->classroom=$dd["classroom"];
          $aa=array("code"=>200,"userinfo"=>$this );
         return json_encode($aa);
      }
      else
        return json_encode(array("code"=> Contains::$LOGIN_FAILD,"message"=>"用户名或者密码错误"));
    }
    public function jion(){
        if(self::$db==null)
            self::$db =new mydb();
        $sql="insert into user(student_id,`password`,username,academy,professional,classroom) values('".$this->student_id."','".$this->pwd."','".$this->name."','".$this->academy."','".$this->professional."','".$this->classroom."');";
        if(self::$db->exec($sql))
            return 1;
        else
            return 0;
    }
    public function changepwd($array) {
         if(self::$db==null)
            self::$db =new mydb();
        $sql="update user  set password='".$array["pwd"]."' where student_id='".$array["id"]."';";
        echo $sql;
         if(self::$db->exec($sql))
            return 1;
        else
            return 0;
    }
}


